package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;

import cn.itcast.erp.entity.Goodstype;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.exception.AuthenticationException;
import cn.itcast.erp.service.GoodstypeService;
import org.apache.commons.io.IOUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;


@RequestMapping("/goodstype")
@RestController
public class GoodstypeController {
    @Resource
    private GoodstypeService goodstypeService;

    //添加
    @CheckPower("商品类型")
    @RequestMapping("/insert")
    public AjaxResult insertGoodstype(@Validated Goodstype goodstype) {
        AjaxResult ar = new AjaxResult();
        this.goodstypeService.insertGoodstype(goodstype);
        ar.setMsg("添加成功");
        return ar;
    }

    //删除
    @CheckPower("商品类型")
    @RequestMapping("/delete")
    public AjaxResult deleteGoodstype(Long uuid) {
        AjaxResult ar = new AjaxResult();
        int i = goodstypeService.deleteGoodstype(uuid);
        if (i > 0) {
            ar.setMsg("删除成功");
        } else {
            ar.setMsg("删除失败");
        }
        return ar;
    }

    //修改
    @CheckPower("商品类型")
    @RequestMapping("/update")
    public AjaxResult updateGoodStypeBy(@Validated Goodstype goodstype) {
        AjaxResult ar = new AjaxResult();
        this.goodstypeService.updateGoodstype(goodstype);
        ar.setMsg("修改成功");
        return ar;

    }

    //按条件查询
    @CheckPower("商品类型")
    @RequestMapping("/find")
    public AjaxResult findGoodstype(Goodstype goodstype) {
        return goodstypeService.findGoodstype(goodstype);
    }


    //查询所有
    @CheckPower("商品类型")
    @RequestMapping("/findAll")
    public List <Goodstype> findAllGoodstype() {
        return this.goodstypeService.findAllGoodstype();
    }


    //查询By id
    @CheckPower("商品类型")
    @RequestMapping("/findById")
    public Goodstype findGoodstypeById(Long uuid) {
        return this.goodstypeService.findGoodstypeById(uuid);
    }


    //导出Excel
    @CheckPower("商品类型")
    @RequestMapping("/export")
    public void getAllGoodstypeEx(HttpServletResponse response) {
        try {
            //清除首部的空白行
            response.reset();
            response.setContentType("application/msexcel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("商品类型信息.xls", "UTF-8"));
            InputStream inputStream = goodstypeService.getAllGoodstypeEx();
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}